package org.xjtu.weteam.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjtu.weteam.mapper.ProjectMapper;
import org.xjtu.weteam.model.domain.Project;
import org.xjtu.weteam.model.domain.Task;
import org.xjtu.weteam.model.dto.ProjectDTO;
import org.xjtu.weteam.service.ProjectService;
import org.xjtu.weteam.service.TaskService;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private TaskService taskService;

    @Override
    public Project projectSearchById(Integer projectId) {
        return projectMapper.projectSearchById(projectId);
    }

    @Override
    public List<Project> listProjects(Integer teamId) {
        return projectMapper.listProjects(teamId);
    }

    /**
     * 添加项目
     * @param project
     * @return
     */
    @Override
    public int insertProject(Project project) {
        return projectMapper.insertProject(project);
    }

    @Override
    public List<ProjectDTO> selectProjectAndItsNum(Integer teamId) {
        List<Project> projects = this.listProjects(teamId);
        List<ProjectDTO> projectDTOS = new ArrayList<>();
        for(int i=0;i<projects.size();i++){
            Project a = projects.get(i);
            ProjectDTO single = new ProjectDTO().setProjectId(a.getProjectId()).setProjectName(a.getProjectName()).setStatus(a.getStatus());
            List<Task> tasks = taskService.taskSearchAll(a.getProjectId());
            for(int j = 0; j<tasks.size();j++){
                Task t = tasks.get(j);
                //全部任务数
                if(!t.getStatus().equals("113")){
                    single.setTaskNum(single.getTaskNum()+1);
                }
                //今日新增
                if(isXday(t.getCreateTime(),0) && !t.getStatus().equals("113")){
                    single.setTodayTaskNum(single.getTodayTaskNum()+1);
                }
                //昨日新增
                if(isXday(t.getCreateTime(),-1) && !t.getStatus().equals("113")){
                    single.setYesTaskNum(single.getYesTaskNum()+1);
                }
                //已完成
                if(t.getStatus().equals("111")) {
                    single.setFinish(single.getFinish() + 1);
                }
            }
            projectDTOS.add(single);
        }
        return projectDTOS;
    }

    @Override
    public int changeStatus(Integer projectId, String newStatus) {

        return projectMapper.updateStatus(projectId, newStatus);
    }

    /**
     * 获取昨天的开始时间戳
     */
    private static Timestamp getXStart(Integer x) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, x);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 获取昨天的结束时间戳
     */
    private static Timestamp getXEnd(Integer x) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DATE, x);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return new Timestamp(calendar.getTimeInMillis());
    }

    /**
     * 判断时间戳是否是昨天
     */
    private static boolean isXday(Timestamp timestamp, Integer x) {
        Timestamp yesterdayStart = getXStart(x);
        Timestamp yesterdayEnd = getXEnd(x);
        return timestamp.compareTo(yesterdayStart) >= 0 &&
                timestamp.compareTo(yesterdayEnd) <= 0;
    }


}
